{% extends 'community/basic.html' %}
{% load static %}
{% block title %}用户注册{% endblock title %}

{% block css %}
    <link rel="stylesheet" href="{% static 'community/css/account.css' %}">
{% endblock css %}

{% block content %}
<div class="account">
    <div class="title">用户注册</div>
    <form id="regForm" method="post" novalidate>
        {% csrf_token %}
        {% for field in form %}
            {% if field.name == 'code' %}
                <div class="form-group">
                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                    <div class="row">
                        <div class="col-xs-7">
                            {{ field }}
                            <span class="error-msg">{{ field.errors.0 }}</span>
                        </div>
                        <div class="col-xs-5">
                            <input id="btnSms" type="button" class="btn btn-default" value="获取手机验证码" />
                        </div>
                    </div>
                </div>
            {% else %}
                <div class="form-group">
                    <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                    {{ field }}
                    <span class="error-msg">{{ field.errors.0 }}</span>
                </div>
            {% endif %}
        {% endfor %}
        <div class="row">
            <div class="col-xs-3">
                <input id="btnSubmit" type="button" class="btn btn-primary" value="注 册" />
            </div>
        </div>
    </form>
</div>
{% endblock content %}

{% block js %}
    <script>
        $(function () {
            bindClickBtnSms();
            bindClickSubmit();
        });
        // 注册
        function bindClickSubmit(){
            $('#btnSubmit').click(function(){
                // 数据ajax发送到后台
                $.ajax({
                    url: "{% url 'community:register' %}",
                    type: "POST",
                    data: $('#regForm').serialize(), // 拿到所有input的数据 + csfr token
                    dataType: "JSON",
                    success: function(res){
                        console.log(res);
                    }
                })

            })
        }
        // 点击发送并获取验证码
        function bindClickBtnSms(){
            $('#btnSms').click(function(){
                $('.error-msg').empty();
                // 获取手机号
                var mobilePhone = $('#id_mobile_phone').val();
                // 发送ajax, 发送手机号
                $.ajax({
                    url: '{% url 'community:send_sms' %}',
                    type: 'GET',
                    data: {mobile_phone: mobilePhone, tpl: 'register'},
                    dataType: 'JSON',  //数据字典化
                    success: function (res) {
                        //ajax请求成功, 执行函数; res为后端传送的数据
                        if(res.status){
                             console.log('发送成功');
                             sendSmsRemind();
                        } else {
                             $.each(res.error, function (key, value){  // each表示循环error的内容key value
                                $("#id_"+key).next().text(value[0]);
                             })
                        }
                    }
                })
            })
        }
        // 发送验证码后倒计时
        function sendSmsRemind(){
            var $smsBtn = $('#btnSms');
            $smsBtn.prop('disabled', true); // 禁用
            var time = 60;  // 限定等待时长60s
            // 定时器
            var remind = setInterval(function(){
                $smsBtn.val(time + '秒后重新发送');
                time = time - 1;
                if(time < 1){
                    clearInterval(remind);
                    $smsBtn.val('重新获取手机验证码').prop('disabled', false);
                }
            }, 1000)
        }
    </script>
{% endblock js %}